package com.aube.cosplay.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.huyn.baseframework.utils.Constant;
import com.huyn.baseframework.utils.Utils;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth.Resp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		api = WXAPIFactory.createWXAPI(this, Constant.APP_ID, false);

        api.handleIntent(getIntent(), this);
    }
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		
		setIntent(intent);
        api.handleIntent(intent, this);
	}
	
	@Override
	public void onReq(BaseReq arg0) {
		finish();
	}

	@Override
	public void onResp(BaseResp resp) {
		if(resp == null) {
			finish();
			return;
		}
		String result = "";
		if(resp instanceof Resp) {
			switch (resp.errCode) {
				case BaseResp.ErrCode.ERR_OK:
					Resp authResp = (Resp) resp;

					Intent intent = new Intent(Constant.AUTH_SUCCESS_FROM_WX);
					intent.putExtra(Constant.AUTH_CODE, authResp.code);
					intent.putExtra(Constant.AUTH_URL, authResp.url);
					intent.putExtra(Constant.AUTH_STATE, authResp.state);

					Utils.Log("WXINFO", authResp.code);

					sendBroadcast(intent);
					break;
				case BaseResp.ErrCode.ERR_USER_CANCEL:
					result = "登录取消";
					break;
				case BaseResp.ErrCode.ERR_AUTH_DENIED:
					result = "认证失败";
					break;
				default:
					result = "未知错误";
					break;
			}

//			if(StringUtils.isNotBlank(result))
//				ShareUtils.showNotification(getApplicationContext(), 1000, result);
//			ShareUtils.showNotification(getApplicationContext(), 1000, "类型错误");
		}
		
		finish();
	}
	
}
